package com.isunland.managesystem.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class BaseRestartDialogFragment extends DialogFragment {
  private static final String EXTRA_HINT_ID = "com.isunland.managesystem.ui.EXTRA_HINT_ID";
  private static final String EXTRA_HINT_STRING = "com.isunland.managesystem.ui.EXTRA_STRING";
  private static final String EXTRA_TITLE_ID = "com.isunland.managesystem.ui.EXTRA_TITLE_ID";
  private int mHintId;
  private int mTitleId;
  private String mHintString;

  @Override public void onCreate(Bundle savedInstanceState) {
    mHintId = getArguments().getInt(EXTRA_HINT_ID);
    mTitleId = getArguments().getInt(EXTRA_TITLE_ID);
    mHintString = getArguments().getString(EXTRA_HINT_STRING);
    super.onCreate(savedInstanceState);
  }

  @Override public Dialog onCreateDialog(Bundle savedInstanceState) {

    Builder builder = new AlertDialog.Builder(getActivity());
    if (mTitleId != 0) {
      builder.setTitle(mTitleId);
    }
    if (mHintId != 0) {
      builder.setMessage(mHintId);
    }
    if (mHintString != null) {
      builder.setMessage(mHintString);
    }
    builder.setPositiveButton(android.R.string.ok, new OnClickListener() {

      @Override public void onClick(DialogInterface dialog, int which) {
        if (getTargetFragment() == null) return;
        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
      }
    }).setNegativeButton(android.R.string.cancel, new OnClickListener() {

      @Override public void onClick(DialogInterface dialog, int which) {

      }
    });
    return builder.create();
  }

  /**
   * @param hintStringId 提示信息的id
   */
  public static BaseRestartDialogFragment newInstance(int hintStringId) {
    Bundle argsBundle = new Bundle();
    argsBundle.putInt(EXTRA_HINT_ID, hintStringId);
    BaseRestartDialogFragment fragment = new BaseRestartDialogFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  /**
   * @param hintStringId 提示信息的id
   * @param titleId 标题id
   */
  public static BaseRestartDialogFragment newInstance(int hintStringId, int titleId) {
    Bundle argsBundle = new Bundle();
    argsBundle.putInt(EXTRA_HINT_ID, hintStringId);
    argsBundle.putInt(EXTRA_TITLE_ID, titleId);
    BaseRestartDialogFragment fragment = new BaseRestartDialogFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }
}
